package cn.pping.system.service.impl;

import cn.pping.system.domain.Role;
import cn.pping.system.mapper.RoleMapper;
import cn.pping.system.service.IRoleService;
import cn.pping.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pping
 * @since 2022-08-02
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Transactional
    public void add(Role role) {
        // 三张表   role(返回自增的id)
        roleMapper.add(role);
        if(role.getPermissions().size()>0){
            // 添加t_role_permission
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size()>0){
            // 添加t_role_menu
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }
    }

    @Override
    public void update(Role role) {
        // 修改
        roleMapper.update(role);

        // 先删除，再添加
        roleMapper.removeRolePermission(role.getId());
        roleMapper.removeRoleMenu(role.getId());

        // 添加操作
        if(role.getPermissions().size()>0){
            // 添加t_role_permission
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size()>0){
            // 添加t_role_menu
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }



    }

    @Override
    @Transactional
    public void remove(Long rid) {
        // 删除 ， 先删除关联表，再删除自己

        roleMapper.removeEmployeeRole(rid);
        roleMapper.removeRoleMenu(rid);
        roleMapper.removeRolePermission(rid);

        // 删除自己
        roleMapper.remove(rid);

    }
}
